<?php
include("include/config.inc.php");

if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{	
	$billMasterArr = 0;
  $masterArray   = array();
  $detailArray   = array();
  $totalAmm      = 0 ;
  $totalAll      = 0 ;
  $tax           = 0 ;
  $allTax        = 0 ;
  $serviceTax    = 0 ;
  $AllserviceTax = 0 ;
  $roundOff      = 0 ;
  $netTotal      = 0 ; 
  
  
	
	if(isset($_GET['detail']) && isset($_GET['billMasterId']) && $_GET['billMasterId'] > 0)
	{
 	  $selBillMaster = "SELECT billMasterId, billNo, DATE_FORMAT(date,'%d-%m-%Y') AS billDate, partyName, total, taxInPrecentage, serviceTax, roundOff, netTotal, notes
		                    FROM billmaster
		                    JOIN party ON billmaster.partyId = party.partyId
		                   WHERE billMasterId = ".$_GET['billMasterId'];
    $selBillMasterRes = mysql_query($selBillMaster);
    while($mRow = mysql_fetch_array($selBillMasterRes))
    {
      $masterArray['billMasterId']    = $mRow['billMasterId'];
      $masterArray['billNo']     = $mRow['billNo'];
      $masterArray['billDate']   = $mRow['billDate'];
      $masterArray['partyName']  = $mRow['partyName'];
      $masterArray['total']      = $mRow['total'];
      $masterArray['taxInPrecentage'] = $mRow['taxInPrecentage'];
      $masterArray['serviceTax'] = $mRow['serviceTax'];
      $masterArray['roundOff']   = $mRow['roundOff'];
      $masterArray['netTotal']   = $mRow['netTotal'];
      $masterArray['notes']      = $mRow['notes'];
      $serviceTax                = $mRow['taxInPrecentage'];
      $roundOff                  = $mRow['roundOff'];
      $netTotal                  = $mRow['netTotal'];

      
      $selBillDetail = "SELECT billdetail.*, DATE_FORMAT(outgoingmaster.date,'%d-%m-%y') AS outDate, outChallenNo, incoming.challan AS inChallenNo,materialName
                          FROM billdetail
                          JOIN outgoingmaster ON billdetail.outGoingMasterId = outgoingmaster.outGoingMasterId
                          JOIN incoming ON outgoingmaster.incomingId = incoming.incomingId
                          JOIN material ON incoming.materialId = material.materialId
                         WHERE billMasterId = ".$mRow['billMasterId'];
      $selBillDetailRes = mysql_query($selBillDetail);
      $dCount = 0;
      while($dRow = mysql_fetch_array($selBillDetailRes))
      {
      	$detailArray[$dCount]['billDetailId']      = $dRow['billDetailId'];
      	$detailArray[$dCount]['billMasterId']      = $dRow['billMasterId'];
      	$detailArray[$dCount]['outDate']           = $dRow['outDate'];
      	$detailArray[$dCount]['outChallenNo']      = $dRow['outChallenNo'];
      	$detailArray[$dCount]['inChallenNo']       = $dRow['inChallenNo'];
      	$detailArray[$dCount]['materialName']      = $dRow['materialName'];
      	$detailArray[$dCount]['finalProcess']      = $dRow['finalProcess'];
      	$detailArray[$dCount]['outQuantity']       = $dRow['outQuantity'];
      	$detailArray[$dCount]['outWieght']         = $dRow['outWieght'];
      	$detailArray[$dCount]['outRate']           = $dRow['outRate'];
      	$detailArray[$dCount]['amount']            = $dRow['amount'];
      	$totalAll                                 += $dRow['amount'];
      	$tax                                       = $mRow['taxInPrecentage'];
      	$roundOff                                  = $mRow['roundOff'];
      	$dCount++;
      }
    }
    $dCount++;
    $allTax   = $tax;
    $totalAmm = $totalAll;
    $serviceTax   =  $tax * $totalAmm /100 ;
    $roundOff = $roundOff;
    $netTotal = $totalAmm + $serviceTax + $roundOff ;
	}
	
	
	if(isset($_POST['partyId']) && $_POST['partyId'] > 0)
	{
    $selectBill = "SELECT billMasterId, billNo, DATE_FORMAT(date,'%d-%m-%Y') AS billDate, partyName, total, serviceTax, roundOff, netTotal, notes
		                 FROM billmaster
		                 JOIN party ON billmaster.partyId = party.partyId
		                WHERE billmaster.partyId = ".$_POST['partyId'];
    $selectBillRes = mysql_query($selectBill);
    $billMasterArr = array();
    $cnt = 0;
    while($billRow = mysql_fetch_array($selectBillRes))
    {
    	$billMasterArr[$cnt]['billMasterId'] = $billRow['billMasterId'];
    	$billMasterArr[$cnt]['billNo']       = $billRow['billNo'];
    	$billMasterArr[$cnt]['billDate']     = $billRow['billDate'];
    	$billMasterArr[$cnt]['partyName']    = $billRow['partyName'];
    	$billMasterArr[$cnt]['total']        = $billRow['total'];
    	$billMasterArr[$cnt]['serviceTax']   = $billRow['serviceTax'];
    	$billMasterArr[$cnt]['roundOff']     = $billRow['roundOff'];
    	$billMasterArr[$cnt]['netTotal']     = $billRow['netTotal'];
    	$billMasterArr[$cnt]['notes']        = $billRow['notes'];
    	$cnt++;
    }
	}
	
  $k = 0;
  $partyArray = array();
  $partyId     = isset($_REQUEST['partyId']) ? $_REQUEST['partyId'] : 0;
  $selectParty = "SELECT partyId,partyName
                    FROM party
                   WHERE partyId IN (SELECT partyId FROM billmaster)
                   ORDER BY partyName";
  $selectPartyResult = mysql_query($selectParty);
  while($partyRow = mysql_fetch_array($selectPartyResult))
  {
    $partyArray['partyId'][$k]   = $partyRow['partyId'];
    $partyArray['partyName'][$k] = $partyRow['partyName'];
    $k++;
  }
  include("./bottom.php");
  $smarty->assign("billMasterArr",$billMasterArr);
  $smarty->assign("partyId",$partyId);
  $smarty->assign("partyArray",$partyArray);
  $smarty->assign("totalAmm",$totalAmm);
  $smarty->assign("allTax",$allTax);
  $smarty->assign("serviceTax",$serviceTax);
  $smarty->assign("roundOff",$roundOff);
  $smarty->assign("netTotal",$netTotal);
  $smarty->assign("masterArray",$masterArray);
  $smarty->assign("detailArray",$detailArray);

  if(isset($_GET['detail']))
  {
    $smarty->display("billListDetail.tpl");
  }
  else
  {
    $smarty->display("billList.tpl");
  }
}
?>